package org.darkforest.ucenter.service.impl;

import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.mail.MailUtil;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.Page;
import org.darkforest.common.util.SmsSendUtil;
import org.darkforest.common.util.SystemConstant;
import org.darkforest.dao.mapper.*;
import org.darkforest.enums.MemberType;
import org.darkforest.enums.OrderLogEnum;
import org.darkforest.model.*;
import org.darkforest.ucenter.service.MemberService;
import org.darkforest.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * 项目名称         :bwmall-ucenter
 * 文件名           :MemberServiceImpl.java
 * 开发公司及作者    :陈超
 * 版本号及开发日期  :Rev. 1.00 (2015年2月7日)
 */

@Service("memberServiceImpl")
public class MemberServiceImpl implements MemberService {
    Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);
    @Autowired
    protected MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberCommonMapper memberCommonMapper;
    @Autowired
    private PdLogMapper pdLogMapper;
    @Autowired
    private SettingMapper settingMapper;
    @Autowired
    private MemberMsgTplMapper memberMsgTplMapper;

    private static String[] keyList = new String[]{"memberEmailPrivacy",
            "memberTruenamePrivacy", "memberSexPrivacy",
            "memberBirthdayPrivacy", "areaPrivacy", "qqPrivacy",
            "wwPrivacy",};

    @Override
    public MemberWithBLOBs findMemberById(Integer memberId) {
        MemberWithBLOBs m = memberMapper.selectByPrimaryKey(memberId);
        parseMemberPrivacy(m);
        return m;
    }

    @Override
    public Member findSimpleMemberById(Integer memberId) {
        return memberMapper.getMemberById(memberId);
    }

    @Override
    public Map<String, Object> getOrderSimpleCount(Integer memberId) {
        List<Map<String, Object>> res = orderMapper.selectByOrderSimpleCount(memberId);
        Map<String, Object> map = new HashMap<String, Object>();
        for (Map<String, Object> m : res) {
            String state = (String) m.get("order_state");
            if (state != null) {
                if ("10".equals(state.trim())) {
                    map.put("payCount", m.get("cc"));
                } else if ("30".equals(state.trim())) {
                    map.put("recCount", m.get("cc"));
                } else if ("40".equals(state.trim())) {
                    m.put("comCount", m.get("cc"));
                }
            }
        }
        return map;
    }

    @Override
    public int updateMember(MemberWithBLOBs vo) {
        initMemberPrivacy(vo);
        return memberMapper.updateByPrimaryKeySelective(vo);
    }

    public void initMemberPrivacy(MemberWithBLOBs vo) {
        JSONObject json = new JSONObject();
        for (String key : keyList) {
            try {
                json.put(key, BeanUtils.getProperty(vo, key));
            } catch (IllegalAccessException e) {

            } catch (InvocationTargetException e) {

            } catch (NoSuchMethodException e) {

            }
        }
        vo.setMemberPrivacy(json.toString());
    }

    public void parseMemberPrivacy(MemberWithBLOBs vo) {
        JSONObject json;
        if (vo == null || StringUtils.isBlank(vo.getMemberPrivacy())) {
            json = new JSONObject();
        } else {
            json = JSONObject.fromObject(vo.getMemberPrivacy());
        }
        for (String key : keyList) {
            try {
                if (json.containsKey(key)) {
                    BeanUtils.setProperty(vo, key, json.getString(key));
                } else {
                    BeanUtils.setProperty(vo, key, "0");
                }
            } catch (IllegalAccessException e) {

            } catch (InvocationTargetException e) {

            }
        }
    }

    /**
     * @param userName
     * @return
     */
    public Member getBaseMemberInfo(String userName) {
        if (userName == null || "".equals(userName)) {
            return null;
        }
        return memberMapper.selectByUserName(userName);
    }

    @Override
    public WapAjaxMsg<MemberBase> regist(Member member) {
        WapAjaxMsg<MemberBase> msg = new WapAjaxMsg<MemberBase>();
        //信息不完全直接返回
        if (member.getMemberName() == null || member.getMemberPasswd() == null || member.getMemberPasswdConfirm() == null ||
                (member.getMemberEmail() == null && member.getMemberMobile() == null)) {
            msg.setError("注册信息不完全");
            return msg;
        }
        if (!member.getMemberPasswd().equals(member.getMemberPasswdConfirm())) {
            msg.setError("密码与注册密码不相同");
            return msg;
        }
        if (getBaseMemberInfo(member.getMemberName()) != null) {
            msg.setError("用户名已存在");
            return msg;
        }

        // memberName、memberPasswd、memberEmail
        MemberWithBLOBs registor = new MemberWithBLOBs(member);
        //member_truename、member_avatar、member_sex、member_birthday
        registor.setMemberPasswd(DigestUtils.md5Hex(member.getMemberPasswd()));        //对密码进行md5加密
        //member_paypwd
        registor.setMemberEmail(member.getMemberEmail()!=null?member.getMemberEmail():"");
        registor.setMemberEmailBind(member.getMemberEmailBind()!=null?member.getMemberEmailBind():0);
        registor.setMemberMobile(member.getMemberMobile()!=null?member.getMemberMobile():"");
        registor.setMemberMobileBind(member.getMemberMobileBind()!=null?member.getMemberMobileBind():0);
        // member_qq、member_ww
        registor.setMemberLoginNum(0);
        registor.setMemberTime(DateUtil.unixTimeStamp());
        registor.setMemberLoginTime(DateUtil.unixTimeStamp());
        registor.setMemberOldLoginTime(DateUtil.unixTimeStamp());
        // member_login_ip、member_old_login_ip、member_qqopenid、member_qqinfo
        // member_sinaopenid、member_sinainfo
        registor.setMemberPoints(0);
        registor.setAvailablePredeposit(new BigDecimal(0));
        registor.setFreezePredeposit(new BigDecimal(0));
        registor.setInformAllow(true);
        registor.setIsBuy(true);
        registor.setIsAllowtalk(true);
        registor.setMemberState(true);
        registor.setMemberCredit(0);
        registor.setMemberSnsvisitnum(0);
        // member_areaid、member_cityid、member_provinceid、member_areainfo、
        // member_privacy、member_quicklink、
        registor.setMemberCoin(new BigDecimal(0));
        // member_wxopenid、member_wxnickname、
        registor.setMemberType(member.getMemberType());
        // path、employee_level、recommend_id、recommend_name、freeze_points
        // proxy_path、member_level、member_freeze_coin、pv_consume、consume_amt、layer
        registor.setCompanyId(member.getCompanyId());
        memberMapper.insert(registor);
        logger.debug("member_id:" + registor.getMemberId());
        if (registor.getMemberId() != null) {
            //数据库插入成功，赋值返回（wap端需获取token）
            msg.setDatas(new MemberBase(registor.getMemberId(), member.getMemberName()));
        }
        return msg;
    }

    /**
     * 登录，登录成功
     *
     * @param userName
     * @param pwd
     * @return
     */
    @Override
    public WapAjaxMsg<MemberBase> login(String userName, String pwd) {
        WapAjaxMsg<MemberBase> msg = new WapAjaxMsg<MemberBase>();
        msg.setDatas(new MemberBase());
        Member member_db = getBaseMemberInfo(userName);
        if (member_db == null) {
            msg.setCode("201");
            msg.setError("用户不存在");
        } else if (DigestUtils.md5Hex(pwd).equals(member_db.getMemberPasswd())) {
            //密码正确（wap端需要生成新的token），返回用户对象
            msg.setDatas(new MemberBase(member_db.getMemberId(), member_db.getMemberName()));
            msg.getDatas().setEmail(member_db.getMemberEmail());
            msg.getDatas().setAvatar(member_db.getMemberAvatar());
            msg.getDatas().setTrueName(member_db.getMemberTruename());
            msg.getDatas().setQuicklink(member_db.getMemberQuicklink()!=null?member_db.getMemberQuicklink():"");
//            msg.getDatas().setMemberType(member_db.getMemberType());
//            msg.getDatas().setProxy_id(member_db.getProxyId());
//            msg.getDatas().setTeam_proxy_id(member_db.getProxyTeamId());

        } else {
            msg.setCode("202");
            msg.setError("用户名密码不正确");
        }
        return msg;
    }

    /**
     * 登录，登录成功
     *
     * @param userName
     * @param pwd
     * @return
     */
    @Override
    public WapAjaxMsg<MemberBase> login(String userName, String pwd,String ip) {
        WapAjaxMsg<MemberBase> msg = new WapAjaxMsg<MemberBase>();
        msg.setDatas(new MemberBase());
        Member member_db = getBaseMemberInfo(userName);
        if (member_db == null) {
            msg.setError("用户不存在");
        } else if (DigestUtils.md5Hex(pwd).equals(member_db.getMemberPasswd())) {
            //密码正确（wap端需要生成新的token），返回用户对象
            msg.setDatas(new MemberBase(member_db.getMemberId(), member_db.getMemberName()));
            msg.getDatas().setEmail(member_db.getMemberEmail());
            msg.getDatas().setAvatar(member_db.getMemberAvatar());
            msg.getDatas().setTrueName(member_db.getMemberTruename());
            msg.getDatas().setQuicklink(member_db.getMemberQuicklink()!=null?member_db.getMemberQuicklink():"");

            member_db.setMemberOldLoginTime(member_db.getMemberLoginTime()!=null?member_db.getMemberLoginTime():"");
            member_db.setMemberLoginTime(DateUtil.toTimeString(new Date()));
            member_db.setMemberOldLoginIp(member_db.getMemberLoginIp()!=null?member_db.getMemberLoginIp():"");
            member_db.setMemberLoginIp(ip);
            member_db.setMemberLoginNum(member_db.getMemberLoginNum()!=null?member_db.getMemberLoginNum()+1:1);
            memberMapper.updateMemberBaseInfo(member_db);
        } else {
            msg.setError("用户名密码不正确");
        }
        return msg;
    }
    public Member checkUser(String memberName) {
        Member member_db = getBaseMemberInfo(memberName);
        if (member_db == null) {
            return null;
        }
        return member_db;
    }

    public Member checkUserAndPassword(String memberName, String password) {
        Member member_db = getBaseMemberInfo(memberName);
        if (member_db == null) {
            return null;
        }
        if (DigestUtils.md5Hex(password).equals(member_db.getMemberPasswd())) {
            return member_db;
        }
        return null;
    }

    @Override
    public List<Member> findByCondition(Member condition) {
        if (condition == null) {
            return Collections.emptyList();
        }

        return memberMapper.getMemberInfoByCond(condition);
    }
    @Override
    public List<Member> getMemberByCond(Member condition) {
        return memberMapper.getMemberByCond(condition);
    }


    /**
     * 注销账户
     *
     * @param userName
     * @param key
     * @return
     */
    @Override
    public WapAjaxMsg<MemberBase> logout(String userName, String key) {
        WapAjaxMsg msg = new WapAjaxMsg();
        if (userName == null || key == null) {
            msg.setError("参数错误");
        }
        msg.setDatas(new MemberBase(userName, key));
        return msg;
    }

    @Override
    public void updatePic(Integer memberId, String picName) {
        if (picName != null && !picName.trim().equals("")) {
            MemberWithBLOBs record = new MemberWithBLOBs();
            record.setMemberId(memberId);
            record.setMemberAvatar(picName);
            memberMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void sendMailCode(Integer memberId, String email, String basePath) {
        String code = insertCheckCode(memberId, email, null);
        MemberMsgTpl memberMsgTpl=memberMsgTplMapper.selectByPrimaryKey("bind_email");
        Setting setting =settingMapper.selectByPrimaryKey("site_name");
        Setting pcSite =settingMapper.selectByPrimaryKey("pcweb_site");
        String url =""+pcSite.getValue()+"/userCenter/checkEmail?code="+code;
        if(memberMsgTpl != null) {
            if (memberMsgTpl.getMmtMailSwitch() == 1 && memberMsgTpl.getMmtMailContent() != null) {
                String[] str = new String[2];
                str[0] = url;
                str[1] =url;
                String context = contextRepalce(memberMsgTpl.getMmtMailContent(),str);
                String[] titles = new String[1];
                titles[0] = setting.getValue();
                String title = contextRepalce(memberMsgTpl.getMmtMailSubject(),titles);
                if ("465".equals(SystemConstant.PORT.trim())) {
                    MailUtil.sendMailBySSL(title, context, SystemConstant.USERNAME.trim(), email);
                } else {
                    // 25
                    MailUtil.sendMail(title, context, SystemConstant.USERNAME.trim(), email);
                }
            }
        }
    }

    public String contextRepalce(String context,String []str){
        for (int i=0;i<str.length;i++){
            context = context.replace("{"+(i+1)+"}",str[i]);
        }
        return context;
    };

    @Override
    public void sendChangeMailCode(Integer memberId, String email, String basePath) {
        String code = insertCheckCode(memberId, email, null);
        MemberMsgTpl memberMsgTpl=memberMsgTplMapper.selectByPrimaryKey("authenticate");
        Setting setting =settingMapper.selectByPrimaryKey("site_name");
        if(memberMsgTpl != null) {
            if (memberMsgTpl.getMmtMailSwitch() == 1 && memberMsgTpl.getMmtMailContent() != null) {
                String[] str = new String[3];
                str[0] = setting.getValue();
                str[1] = DateUtil.toTimeString(new Date());
                str[2] = code;
                String context = contextRepalce(memberMsgTpl.getMmtMailContent(),str);
                String[] titles = new String[1];
                titles[0] = setting.getValue();
                String title = contextRepalce(memberMsgTpl.getMmtMailSubject(),titles);
                MailUtil.sendMail(title, context, MailUtil.SENDER, email);
            }
        }
//        MailUtil.sendMail("商城验证", code, MailUtil.SENDER, email);
    }

    @Override
    public String checkMail(Integer memberId, String code, String email) {
        return checkCode(memberId, code, email, null, null,null);
    }


    private String insertCheckCode(Integer memberId, String email, String mobile) {
        String x = UUID.randomUUID().toString();
        String base64 = null;
        MessageDigest mdInst;
        try {
            mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(x.getBytes());
            // 获得密文
            byte[] md = mdInst.digest();
            base64 = new String(Base64Utils.encode(md));
        } catch (NoSuchAlgorithmException e) {

        }
        Random r = new Random();
        base64 = r.nextInt(999999) + "";
        MemberCommon c = memberCommonMapper.selectByPrimaryKey(memberId);
        if (c == null) {
            c=new MemberCommon();
            c.setAuthCode(base64);
            c.setMemberId(memberId);
            c.setSendAcodeTime(DateUtil.unixTimeStampInt().intValue());
            memberCommonMapper.insert(c);
        } else {
            c.setAuthCode(base64);
            c.setSendAcodeTime(DateUtil.unixTimeStampInt().intValue());
            memberCommonMapper.updateByPrimaryKey(c);
        }
        MemberWithBLOBs m = new MemberWithBLOBs();
        m.setMemberId(memberId);
        m.setMemberEmail(email);
        m.setMemberMobile(mobile);
        memberMapper.updateByPrimaryKeySelective(m);
        return base64;
    }

    @Override
    public void sendMobileCode(Integer memberId, String mobile) {
        String code = insertCheckCode(memberId, null, mobile);
        //发送手机短信
        MemberMsgTpl memberMsgTpl=memberMsgTplMapper.selectByPrimaryKey("authenticate");
        if(memberMsgTpl != null && memberMsgTpl.getMmtShortId()!=0){
            SmsSendUtil.newInstance().send(mobile, memberMsgTpl.getMmtShortId().toString(), new String[]{code, "30"});
        }

    }

    @Override
    public String checkMobile(Integer memberId, String code, String mobile) {
        return checkCode(memberId, code, null, mobile, null,null);
    }


    private String checkCode(Integer memberId, String code, String email, String mobile, String payPwd,String loginPwd) {
        MemberCommon c = memberCommonMapper.selectByPrimaryKey(memberId);
        String mes = null;
        if (c == null) {
            mes = "安全码已经验证过";
        } else {
            if (!code.equals(c.getAuthCode())) {
                mes = "安全码验证失败";
            } else if (DateUtil.unixTimeStampInt() - c.getSendAcodeTime() > 1800) {
                mes = "安全码已过期";
            } else {
                mes = "操作成功";
                MemberWithBLOBs m = new MemberWithBLOBs();
                m.setMemberId(memberId);
                if (email != null) {
                    m.setMemberEmailBind(1);
                    m.setMemberEmail(email);
                } else if (mobile != null) {
                    m.setMemberMobileBind(1);
                    m.setMemberMobile(mobile);
                } else if (payPwd != null) {
                    m.setMemberPaypwd(DigestUtils.md5Hex(payPwd));
                }else if(loginPwd !=null){
                    m.setMemberPasswd(DigestUtils.md5Hex(loginPwd));
                }
                memberMapper.updateByPrimaryKeySelective(m);
            }
        }

        return mes;
    }

    @Override
    public String checkPayPwd(Integer memberId, String code, String pwd) {
        return checkCode(memberId, code, null, null, pwd,null);
    }

    /**
     * admin系统获取会员列表
     * @param param
     * @return
     */
    @Override
    public Page<Member> getMemberListForAdmin(MemberSearchBean param) {
        param.setResultList(memberMapper.getMemberPageList(param));
        return param;
    }

    @Override
    public List<Member> getMemberInfoByCond(Member member) {
        return memberMapper.getMemberInfoByCond(member);
    }

    /**
     * 更新用户类型
     * @param memberId
     * @param memberType
     * @return
     */
    @Override
    public int updateMemberType(Integer memberId, MemberType memberType) {
        if(memberId==null || memberId==0 || memberType==null){
            return 0;
        }
        Member member = new Member();
        member.setMemberId(memberId);
        //member.setMemberType(memberType.ordinal()+1);
        member.setMemberTypeEnum(memberType);
        return memberMapper.updateMemberType(member);
    }


    /**
     * 修改账户基础数据
     * @param member
     * @return
     */
    @Override
    public int updateMemberBaseInfo(Member member) {
        return memberMapper.updateMemberBaseInfo(member);
    }

    @Override
    public MemberWithBLOBs saveWxUserInfo(WxUserDetailInfo userDetailInfo, String recommendId) {
        return null;
    }


    /**
     * 修改用户登录密码
     *
     * @param memberId
     * @param code
     * @param newPwd
     */
    @Override
    public String checkLoginPwd(Integer memberId, String code, String newPwd) {
        return checkCode(memberId, code, null, null, null,newPwd);
    }

    @Override
    public Member getUcenterMemberById(Integer userId) {
        return memberMapper.getMemberById(userId);
    }

    /**
     * 冻结电子币以及可用余额
     * @param memberId
     * @param goodsAmount
     * @return
     */
    @Transactional
    public int freezeMemberDeposit(Integer orderId,String orderSn,Integer memberId, BigDecimal goodsAmount) {
        Member member = memberMapper.getMemberById(memberId);
        if(goodsAmount.compareTo(BigDecimal.ZERO)>0){
            //设置可用额度为冻结额度
            member.setAvailablePredeposit(member.getAvailablePredeposit().subtract(goodsAmount));
            member.setFreezePredeposit(member.getFreezePredeposit().add(goodsAmount));

            PdLog pdLog = new PdLog();                      //生成可用余额日志
            pdLog.setLgAddTime(DateUtil.unixTimeStampInt().intValue());
            pdLog.setLgAvAmount(goodsAmount.negate());
            pdLog.setLgFreezeAmount(goodsAmount);
            pdLog.setLgMemberId(memberId);
            pdLog.setLgMemberName(member.getMemberName());
            pdLog.setLgDesc(OrderLogEnum.CREATE_ORDER.getInfo() + orderSn);
            pdLog.setLgType("deposit");
            pdLog.setLgOrderId(orderId);
            pdLogMapper.insert(pdLog);
        }
        int res = memberMapper.updateMemberAccount(member);
        return res;
    }

    /**
     * 返还多扣除的保证金
     * @param orderId
     * @param memberId
     * @param cash
     * @return
     */
    public int returnDeposit(Integer orderId,String orderSn, Integer memberId,  BigDecimal cash,OrderLogEnum orderLogEnum) {
        Member member = memberMapper.getMemberById(memberId);
        if(cash.compareTo(BigDecimal.ZERO)>0){
            member.setAvailablePredeposit(member.getAvailablePredeposit().add(cash));           //设置冻结额度转为可用额度
            member.setFreezePredeposit(member.getFreezePredeposit().subtract(cash));

            PdLog pdLog = new PdLog();                      //生成可用余额日志
            pdLog.setLgAddTime(DateUtil.unixTimeStampInt().intValue());
            pdLog.setLgAvAmount(cash);
            pdLog.setLgFreezeAmount(cash.negate());
            pdLog.setLgMemberId(memberId);
            pdLog.setLgMemberName(member.getMemberName());
            pdLog.setLgDesc(orderLogEnum.getInfo() + orderSn);
            pdLog.setLgType("order_pay");
            pdLog.setLgOrderId(orderId);
            pdLogMapper.insert(pdLog);
        }
        int res = memberMapper.updateMemberAccount(member);
        return res;
    }

    /**
     * 卖家收到付款，增加可用余额，只针对余额付款使用，如使用在线支付则无该步骤
     * @param orderSn
     * @param memberId
     * @param cash
     */
    public int getPayment(Integer orderId,String orderSn, Integer memberId, BigDecimal cash) {
        Member member = memberMapper.getMemberById(memberId);
        if(cash.compareTo(BigDecimal.ZERO)>0){
            member.setAvailablePredeposit(member.getAvailablePredeposit().add(cash));           //增加可用额度
            PdLog pdLog = new PdLog();                      //生成可用余额日志
            pdLog.setLgAddTime(DateUtil.unixTimeStampInt().intValue());
            pdLog.setLgAvAmount(cash);
            pdLog.setLgFreezeAmount(new BigDecimal(0));
            pdLog.setLgMemberId(memberId);
            pdLog.setLgMemberName(member.getMemberName());
            pdLog.setLgDesc(OrderLogEnum.PAYMENT.getInfo() + orderSn);
            pdLog.setLgType("order_pay");
            pdLog.setLgOrderId(orderId);
            pdLogMapper.insert(pdLog);
        }
        int res = memberMapper.updateMemberAccount(member);
        return res;
    }

    /**
     * 针对使用余额支付取消的情况：
     * 1、买家 冻结余额-->可用余额
     * @param orderSn
     * @param buyerId
     * @param pdAmount
     */
    @Override
    public int returnPayment(Integer orderId,String orderSn, Integer buyerId, BigDecimal pdAmount) {
        Member buyer = memberMapper.getMemberById(buyerId);
//        Member seller = memberMapper.getMemberById(sellerId);
        //买家操作
        if(pdAmount.compareTo(BigDecimal.ZERO)>0){
            buyer.setAvailablePredeposit(buyer.getAvailablePredeposit().add(pdAmount));
            buyer.setFreezePredeposit(buyer.getFreezePredeposit().subtract(pdAmount));
            PdLog pdLog = new PdLog();                      //生成可用余额日志
            pdLog.setLgAddTime(DateUtil.unixTimeStampInt().intValue());
            pdLog.setLgAvAmount(pdAmount);
            pdLog.setLgFreezeAmount(pdAmount.negate());
            pdLog.setLgMemberId(buyerId);
            pdLog.setLgMemberName(buyer.getMemberName());
            pdLog.setLgDesc(OrderLogEnum.PAYMENT_RETURN.getInfo() + orderSn );
            pdLog.setLgType("order_cancel");
            pdLog.setLgOrderId(orderId);
            pdLogMapper.insert(pdLog);
        }
        int res = memberMapper.updateMemberAccount(buyer);
        return res;
    }

    @Override
    public WapAjaxMsg<MemberBase> ThirdPartyRegist(Member member) {
        WapAjaxMsg<MemberBase> msg = new WapAjaxMsg<MemberBase>();
        MemberWithBLOBs registor = new MemberWithBLOBs(member);
        registor.setMemberPasswd(DigestUtils.md5Hex(member.getMemberName()));        //对密码进行md5加密
        registor.setMemberEmailBind(0);
        registor.setMemberMobileBind(0);
        registor.setMemberLoginNum(0);
        registor.setMemberTime(DateUtil.unixTimeStamp());
        registor.setMemberLoginTime(DateUtil.unixTimeStamp());
        registor.setMemberOldLoginTime(DateUtil.unixTimeStamp());
        registor.setMemberPoints(0);
        registor.setAvailablePredeposit(new BigDecimal(0));
        registor.setFreezePredeposit(new BigDecimal(0));
        registor.setInformAllow(true);
        registor.setIsBuy(true);
        registor.setIsAllowtalk(true);
        registor.setMemberState(true);
        registor.setMemberCredit(0);
        registor.setMemberSnsvisitnum(0);
        registor.setMemberCoin(new BigDecimal(0));
        registor.setMemberQqopenid(member.getMemberQqopenid()!=null?member.getMemberQqopenid():"");
        registor.setMemberSinaopenid(member.getMemberSinaopenid()!=null?member.getMemberSinaopenid():"");
        registor.setMemberWxOpenid(member.getMemberWxOpenid()!=null?member.getMemberWxOpenid():"");
        registor.setMemberWxNickName(member.getMemberName()!=null?member.getMemberName():"");
        memberMapper.insert(registor);
        if (registor.getMemberId() != null) {
            //数据库插入成功，赋值返回（wap端需获取token）
            msg.setDatas(new MemberBase(registor.getMemberId(), member.getMemberName()));
        }
        return msg;
    }
}
